package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.PartType;
import com.ruoyi.system.mapper.PartLotMapper;
import com.ruoyi.system.domain.PartLot;
import com.ruoyi.system.service.IPartLotService;
import com.ruoyi.common.core.text.Convert;

/**
 * 停车场信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-30
 */
@Service
public class PartLotServiceImpl implements IPartLotService {
    @Autowired
    private PartLotMapper partLotMapper;

    /**
     * 查询停车场信息
     *
     * @param partLotId 停车场信息主键
     * @return 停车场信息
     */
    @Override
    public PartLot selectPartLotByPartLotId(Long partLotId) {
        return partLotMapper.selectPartLotByPartLotId(partLotId);
    }

    /**
     * 查询停车场信息列表
     *
     * @param partLot 停车场信息
     * @return 停车场信息
     */
    @Override
    public List<PartLot> selectPartLotList(PartLot partLot) {
        return partLotMapper.selectPartLotList(partLot);
    }

    /**
     * 新增停车场信息
     *
     * @param partLot 停车场信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPartLot(PartLot partLot) {
        partLot.setCreateTime(DateUtils.getNowDate());
        int rows = partLotMapper.insertPartLot(partLot);
        insertPartType(partLot);
        return rows;
    }

    /**
     * 修改停车场信息
     *
     * @param partLot 停车场信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePartLot(PartLot partLot) {
        partLot.setUpdateTime(DateUtils.getNowDate());
        partLotMapper.deletePartTypeByPartTypeId(partLot.getPartLotId());
        insertPartType(partLot);
        return partLotMapper.updatePartLot(partLot);
    }

    /**
     * 批量删除停车场信息
     *
     * @param partLotIds 需要删除的停车场信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePartLotByPartLotIds(String partLotIds) {
        partLotMapper.deletePartTypeByPartTypeIds(Convert.toStrArray(partLotIds));
        return partLotMapper.deletePartLotByPartLotIds(Convert.toStrArray(partLotIds));
    }

    /**
     * 删除停车场信息信息
     *
     * @param partLotId 停车场信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePartLotByPartLotId(Long partLotId) {
        partLotMapper.deletePartTypeByPartTypeId(partLotId);
        return partLotMapper.deletePartLotByPartLotId(partLotId);
    }

    /**
     * 新增停车场类型信息
     *
     * @param partLot 停车场信息对象
     */
    public void insertPartType(PartLot partLot) {
        List<PartType> partTypeList = partLot.getPartTypeList();
        Long partLotId = partLot.getPartLotId();
        if (StringUtils.isNotNull(partTypeList)) {
            List<PartType> list = new ArrayList<PartType>();
            for (PartType partType : partTypeList) {
                 partType.setPartTypeId(partLotId);
                list.add(partType);
            }
            if (list.size() > 0) {
                partLotMapper.batchPartType(list);
            }
        }
    }
}
